#./utils/reply.py -- Talking robot reply generating file.
#coding:utf-8


# Import Needs
import inspect
import sys
import random
sys.path.append("..")
from constants import *
from utils.nlp import words


# Fuction: get_reply()
def get_reply(loggerset, answer, mode, i=input): 
    """Give reply from sentence you give.(keyword check)

    Args:
        loggerset (Class: loggerset): The class for log.
        answer (str): The sentence input
        mode (str: mode): The mode clicker.

    Returns:
        tupls: (str:Sentence to reply, str:key)
    """    
    loggerset.log("Searching reply for: "+answer, level=DEBUG, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    if answer == "":
        return "","&"
    answer = answer.lower()
    answer_spllited = words.word_split(loggerset, answer)   # Split into words
    for word in answer_spllited:    # Take words successievly
        for corpus, replies in CORPORA.items():  # Take corpora successievly
            if word == corpus:  # Found corpus
                reply = random.choice(replies)  # Choose reply randomly
                loggerset.log("Giving reply...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
                return reply, "&"+corpus    # Give Reply
            else:
                reply = "&notfound"
    loggerset.log("Reply not found", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    reply = random.choice(CORPORA[reply]) # Choose reply from notfound
    y_n = i("\nHelp me to learn? (y/n)\nanswer>").lower()[0]   # Get input and lower then choose the first character.
    if y_n == "y":  # Help to learn
        keyword = i("\nEnter your keyword.\nanswer>")
        reply = i("\nEnter your reply please.\nanswer>")
        learn(loggerset=loggerset, keyword=keyword, reply=reply)  # Learn from input.
    else:
        keyword = ""
    loggerset.log("Giving reply...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    return reply, "&"+keyword    # Give Reply


# Function: learn()
def learn(loggerset, keyword, reply):
    """Function for Kristina to learn new.

    Args:
        loggerset (Class:loggerset): Class to log.
        keyword (str): The keyword for learning
        reply (str): The sentence for replying.
    """    
    loggerset.log("Learning: "+keyword+", "+reply, level=DEBUG, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    try:
        loggerset.log("Trying to load corpora with: "+keyword, level=DEBUG, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        CORPORA[keyword]  # Try to load with keyword.
    except KeyError:    # Cannot find keyword.
        loggerset.log("Adding new keyword for:"+keyword, level=DEBUG, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        CORPORA[keyword] = [reply]  # Create new reply keyword.
    else:
        CORPORA[keyword].append(reply)
    loggerset.log("Saving corpus...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    save_nlpf("corpora", CORPORA)
    loggerset.log("Learned: "+keyword+","+reply, level=DEBUG, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)